<template>
  <div class="published-list">
    <div class="page-header">
      <h2>已发布文章</h2>
      <div class="header-actions">
        <el-input
          v-model="searchQuery"
          placeholder="搜索文章"
          prefix-icon="Search"
          clearable
          @clear="handleSearch"
          @keyup.enter="handleSearch"
          style="width: 300px"
        />
        <el-button type="primary" @click="$router.push('/editor')">新建文章</el-button>
      </div>
    </div>
    
    <el-table :data="publishedArticles" style="width: 100%">
      <el-table-column prop="title" label="标题" min-width="200">
        <template #default="scope">
          <el-link type="primary" @click="viewArticle(scope.row)">
            {{ scope.row.title }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="发布时间" width="180">
        <template #default="scope">
          {{ formatDate(scope.row.created_at) }}
        </template>
      </el-table-column>
      <el-table-column prop="view_count" label="阅读量" width="100" sortable />
      <el-table-column prop="tags" label="标签" width="200">
        <template #default="scope">
          <el-tag 
            v-for="tag in scope.row.tags" 
            :key="tag"
            size="small"
            style="margin-right: 4px"
          >
            {{ tag }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button-group>
            <el-button size="small" @click="editArticle(scope.row)">编辑</el-button>
            <el-button size="small" type="warning" @click="archiveArticle(scope.row)">归档</el-button>
            <el-button size="small" type="danger" @click="deleteArticle(scope.row)">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search } from '@element-plus/icons-vue'
import dayjs from 'dayjs'
import { fetchArticles, deleteArticle as deleteArticleApi } from '../api/article'

const router = useRouter()
const publishedArticles = ref([])
const searchQuery = ref('')

const formatDate = (date) => {
  return dayjs(date).format('YYYY-MM-DD HH:mm')
}

const viewArticle = (article) => {
  // TODO: 实现文章预览功能
  console.log('查看文章:', article)
}

const editArticle = (article) => {
  router.push(`/editor/${article.id}`)
}

const archiveArticle = async (article) => {
  // TODO: 实现归档功能
  ElMessage.success('归档成功')
}

const deleteArticle = async (article) => {
  try {
    await ElMessageBox.confirm('确定要删除这篇文章吗？', '警告', {
      type: 'warning'
    })
    await deleteArticleApi(article.id)
    ElMessage.success('删除成功')
    loadPublishedArticles()
  } catch (error) {
    if (error !== 'cancel') {
      ElMessage.error('删除失败')
    }
  }
}

const handleSearch = () => {
  loadPublishedArticles()
}

const loadPublishedArticles = async () => {
  try {
    const articles = await fetchArticles()
    publishedArticles.value = articles
      .filter(article => article.status === 'published')
      .filter(article => 
        !searchQuery.value || 
        article.title.toLowerCase().includes(searchQuery.value.toLowerCase())
      )
  } catch (error) {
    ElMessage.error('获取文章列表失败')
  }
}

onMounted(() => {
  loadPublishedArticles()
})
</script>

<style scoped>
.published-list {
  padding: 24px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.page-header h2 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 16px;
  align-items: center;
}
</style> 